package top.lyjwn.todo.common.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.Base64;

/**
 * http图片工具
 *
 * @author luoyujie
 */
public class HttpImageUtil {

    private static final Logger logg = LoggerFactory.getLogger(HttpImageUtil.class);

    /**
     * @param URLName 网络图片地址
     * @param type    图片类型
     * @return String 转换结果
     * @Title getImgeHexString
     * @Description 网络图片转换成二进制字符串
     */
    public static String getImgeHexString(String URLName, String type) {
        String res = null;
        try {
            int HttpResult = 0; // 服务器返回的状态
            URL url = new URL(URLName); // 创建URL
            URLConnection urlconn = url.openConnection(); // 试图连接并取得返回状态码
            urlconn.connect();
            HttpURLConnection httpconn = (HttpURLConnection) urlconn;
            HttpResult = httpconn.getResponseCode();
            if (HttpResult != HttpURLConnection.HTTP_OK) // 不等于HTTP_OK则连接不成功
                logg.info("fail");
            else {
                BufferedInputStream bis = new BufferedInputStream(urlconn.getInputStream());

                BufferedImage bm = ImageIO.read(bis);
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                ImageIO.write(bm, type, bos);
                bos.flush();
                byte[] data = bos.toByteArray();
                res = stringsByImage(data);// 可网页可识别的Base64字符
//				res = byte2hex(data);//二进制字符串
                bos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return res;
    }

    /**
     * byte流转为字符串
     *
     * @param binaryData
     * @return
     */
    public static String stringsByImage(byte[] binaryData) {
        String b = null;
        try {
            // b = decoder.decodeBuffer(base64str);
            b = Base64.getEncoder().encodeToString(binaryData);
        } catch (Exception e) {
            logg.error("stringsByImage Exception", e);
        }
        return b;
    }

    /**
     * @param data     生成图片的二进制字符串
     * @param fileName 图片名称(完整路径)
     * @param type     图片类型
     */
    public static void saveImage(String data, String fileName, String type) {

        BufferedImage image = new BufferedImage(300, 300, BufferedImage.TYPE_BYTE_BINARY);
        ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
        try {
            ImageIO.write(image, type, byteOutputStream);
            // byte[] date = byteOutputStream.toByteArray();
            byte[] bytes = hex2byte(data);
            System.out.println("path:" + fileName);
            RandomAccessFile file = new RandomAccessFile(fileName, "rw");
            file.write(bytes);
            file.close();
        } catch (IOException e) {
            logg.error("saveImage Exception", e);
        }
    }

    /**
     * 反格式化byte
     *
     * @param s
     * @return byte[]
     */
    public static byte[] hex2byte(String s) {
        byte[] src = s.toLowerCase().getBytes();
        byte[] ret = new byte[src.length / 2];
        for (int i = 0; i < src.length; i += 2) {
            byte hi = src[i];
            byte low = src[i + 1];
            hi = (byte) ((hi >= 'a' && hi <= 'f') ? 0x0a + (hi - 'a') : hi - '0');
            low = (byte) ((low >= 'a' && low <= 'f') ? 0x0a + (low - 'a') : low - '0');
            ret[i / 2] = (byte) (hi << 4 | low);
        }
        return ret;
    }

    /**
     * 格式化byte
     *
     * @param b
     * @return
     */
    public static String byte2hex(byte[] b) {
        char[] Digit = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
        char[] out = new char[b.length * 2];
        for (int i = 0; i < b.length; i++) {
            byte c = b[i];
            out[i * 2] = Digit[(c >>> 4) & 0X0F];
            out[i * 2 + 1] = Digit[c & 0X0F];
        }

        return new String(out);
    }

    public static void main(String[] args) {
        String imgeHexString = getImgeHexString("https://wx.qlogo.cn/mmopen/vi_32/NwzJLjZBdzKbwIeywBNB2ooddkgebSJz8E3ZOuKNgnLewKhramHrAwjR26iao2yQpk7omarXuA4SxYIkIxbbxCA/132", "jpg");
        System.out.println(imgeHexString);
        System.err.println("123");
    }
}
